package com.punai.common.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;

import java.util.*;

public class ParseContentUtil {
    public static Map<String, List<String>> getContentObj() {
        Map<String, List<String>> content = new LinkedHashMap<>();
        content.put("gygx", new ArrayList<>());
        content.put("qtnr", new ArrayList<>());
        content.put("bcnr", new ArrayList<>());
        return content;
    }

    public static List<String> distinctList(List<String> strList) {
        if (strList != null) {
            LinkedHashSet<String> hashSet = new LinkedHashSet<>(strList);
            return new ArrayList<>(hashSet);
        } else {
            return null;
        }
    }

    public static Boolean isNotEmpty(List<String> strList){
        if(strList != null && strList.size() > 0){
            return true;
        }else {
            return false;
        }
    }

    public static String parseContent(Map<String, List<String>> parseBo) {
        List<String> gygx = distinctList(parseBo.get("gygx"));
        List<String> qtnr = distinctList(parseBo.get("qtnr"));
        List<String> bcnr = distinctList(parseBo.get("bcnr"));
        StringBuilder sb = new StringBuilder();
        Integer num = 0;
        if (isNotEmpty(gygx)) {
            sb.append("一、工艺工序：");
            for (String s : gygx) {
                num++;
                sb.append("\n").append(num).append("、").append(s).append("；");
            }
            sb.deleteCharAt(sb.length() -1 ).append("。");
        }
        num = 0;
        if (isNotEmpty(qtnr)) {
            if (isNotEmpty(gygx)) {
                sb.append("\n二");
            } else {
                sb.append("一");
            }
            sb.append("、其他内容：");
            for (String s : qtnr) {
                num++;
                sb.append("\n").append(num).append("、").append(s).append("；");
            }
            sb.deleteCharAt(sb.length() -1 ).append("。");
        }
        num = 0;
        if (isNotEmpty(bcnr)) {
            if (isNotEmpty(gygx) && isNotEmpty(qtnr)) {
                sb.append("\n三");
            } else if (isNotEmpty(gygx) || isNotEmpty(qtnr)) {
                sb.append("\n二");
            } else {
                sb.append("一");
            }
            sb.append("、补充内容：");
            for (String s : bcnr) {
                num++;
                sb.append("\n").append(num).append("、").append(s).append("；");
            }
            sb.deleteCharAt(sb.length() -1 ).append("。");
        }
        return sb.toString();
    }

    public static String parseContent(String content) {
        if (StrUtil.isEmpty(content)) {
            return "";
        }
        return parseContent(parseObject(content));
    }

    public static Map<String, List<String>> parseObject(String content){
        return JSON.parseObject(content, LinkedHashMap.class);
    }

    public static String mergeContent(Map<String, List<String>> fbContent, String content){
        Map<String, List<String>> parseBo = ParseContentUtil.parseObject(content);
        if (parseBo.get("gygx") != null) {
            fbContent.get("gygx").addAll(parseBo.get("gygx"));
        }
        if (parseBo.get("qtnr") != null) {
            fbContent.get("qtnr").addAll(parseBo.get("qtnr"));
        }
        if (parseBo.get("bcnr") != null) {
            fbContent.get("bcnr").addAll(parseBo.get("bcnr"));
        }
        return parseContent(fbContent);
    }

    public static String appendContent(String content, List<String> addContent) {
        Map<String, List<String>> parseBo = new LinkedHashMap<>();
        parseBo = JSON.parseObject(content, LinkedHashMap.class);
        parseBo.put("bcnr", addContent);
        return JSON.toJSONString(parseBo);
    }

}
